//获取贴吧里的1楼时间和各个用户的ip
//http://yourdomin.com/author.php?act=insert&author=12121&author_id=1212&ip="212.1.23.*"
//http://yourdomin.com/post.php?act=update&id=1&time=1920-12-12
var request = require('request');
var cheerio = require('cheerio');
var iconv = require('iconv-lite');
var nodegrass = require('nodegrass');
var Url = "http://yourdomin.com/post.php?act=get&id=";
var PostUrl = "http://tieba.baidu.com/p/";
var InsertTimeUrl = "http://yourdomin.com/post.php?act=update&id=";
var InsertAuthor = "http://yourdomin.com/author.php?act=insert";
var fs = require('fs');
var END_ID = 42468; //42468;
var CurrentId = 1;

var iter_img;
var iter_user;


function test() {



}

//test();
getPostHref();
//getPost(42477, 3542767499);

//getImgCode("3540778530", "c2fdfc039245d688dc414458a7c27d1ed31b248e");

function getPostHref() {
    if (CurrentId > END_ID) {
        console.log("----ALL DEAL COMPLETE!!----");
        return;
    }
    request(Url + CurrentId, function (err, resp, body) {
        if (err) {
            getPostHref(); // retry
        } else {
            console.log("CurrentPostId : " + CurrentId + " -  href : " + body);
            getPost(CurrentId, body);
            CurrentId++;
        }
    });
}

function getPost(id, href) {
    request(PostUrl + href, function (err, resp, body) {
        if (err) {
            getPost(id, href); // retry
        } else {
            console.log("CurrentPostHref : " + href);
            getPostTime(id, body, href);
        }
    });
}


function getPostTime(id, page, href) {
    var pos = page.indexOf('post_index&quot;:1}');


    if (pos == -1) {
        getPostHref();
        return;
    }
    var date_str = page.substr(pos - 240, 30);
    if (date_str == null || date_str == undefined || date_str == "") {
        getPostHref();
    }

    var reg = /\d{4}-\d{2}-\d{2} \d{2}:\d{2}/;
    var start = date_str.search(reg);
    var date = date_str.substr(start, 16);
    //console.log(date);
    request(InsertTimeUrl + id + "&time=" + date, function (err, resp, body) {
        if (err) {
            
            try{getPostTime(id, datestr, href); // retry
               }
            catch(e)
            {
                getPostHref();
            }
        } else {
            console.log("update datetime:" + date);
            //fs.appendFile("output.txt",page);
            iter_img = img(page);
            getImgCode(href, "");
        }
    });
}

function * img(body) {
    $ = cheerio.load(body);
    //var reg = /"user_id":\d{5,11},"user_name":".{2,16}","user_ip":"\d{1,3}\.\d{1,3}\.\d{1,3}\.\*"/g;
    var img = $('img.BDE_Image');

    for (var i = 0; i < img.length; i++) {
        //console.log($(img[i]).attr('src'));
        var str = $(img[i]).attr('src');
        var start = str.lastIndexOf("/");
        var end = str.lastIndexOf(".jpg");
        var src = str.substr(start + 1, end - start - 1);
        //console.log(str);
        //console.log(src);
        yield src;
    }
}



function getImgCode(href, imgid) {

    var tmpUrl = "";
    if (imgid != null && imgid != "") {
        tmpUrl = "http://tieba.baidu.com/photo/p?kw=%E9%94%A4%E5%AD%90%E7%A7%91%E6%8A%80&ie=utf-8&flux=1&tid=" + href + "&pic_id=" + imgid;
    } else {

        imgid = iter_img.next().value;
        tmpUrl = "http://tieba.baidu.com/photo/p?kw=%E9%94%A4%E5%AD%90%E7%A7%91%E6%8A%80&ie=utf-8&flux=1&tid=" + href + "&pic_id=" + imgid;
    }


    //百度里人名信息为gbk，而request不识别该种格式，所以指定encoding为null，获取二进制数据流，然后用iconv转为utf-8
    request({
        uri: tmpUrl,
        encoding: null
    }, function (err, resp, body) {
        if (err) {
            getImgCode(href, imgid); // retry
        } else {
            var data = iconv.decode(body, 'gbk');
            //fs.appendFile("output.txt", data);
            var reg = /"user_id":\d{5,11},"user_name":".{2,16}","user_ip":"\d{1,3}\.\d{1,3}\.\d{1,3}\.\*"/g;
            //reg = /loor/g;
            //fs.writeFile("img.html", body);
            var match_res = data.match(reg);
            if (match_res == null) {
                getPostHref();
                return;
            }

            iter_user = retIdIp(match_res);
            getNextUser("");
        }
    });
}

function getNextUser(url) {
    if (url != "") {
        request(encodeURI(InsertAuthor + url), function (err, resp, body) {
            if (err) {
                getNextUser(url); // retry
            } else {
                console.log("Insert ok:" + url);
                getNextUser("");
            }
        });
    }

    var o = iter_user.next();
    if (o.done == false) {
        request(encodeURI(InsertAuthor + o.value), function (err, resp, body) {
            if (err) {
                getNextUser(o.value); // retry
            } else {
                console.log("Insert ok:" + o.value);
                getNextUser("");
            }
        });
    } else {
        getPostHref()
    }
}

function * retIdIp(match_res) {
    //console.log(match_res);
    for (var i = 0; i < match_res.length; i++) {
        //        id_start_po = match_res[i].indexOf("user_id") + 9;
        //        id_end_po = match_res[i].indexOf("user_name") - 2;
        //        //authot_start_po = match_res[i].indexOf("user_name") + 9;
        //        //author_end_po = match_res[i].indexOf("floor") - 2;
        //        ip_start_po = match_res[i].indexOf("user_ip") + 9;
        //        ip_end_po = match_res[i].indexOf("floor") - 2;
        var jsonStr = "{" + match_res[i] + "}";
        var userinfo = JSON.parse(jsonStr);

        var str = '&author_id=' + userinfo.user_id + '&author=' + userinfo.user_name + '&ip="' + userinfo.user_ip + '"';
        yield str;
        //var s = "&author_id=" + match_res[i].substr(id_start_po, id_end_po - id_start_po) + "&ip=" + match_res[i].substr(ip_start_po, ip_end_po - ip_start_po);
        //console.log("11");
        //fs.appendFile("img.html",match_res[i]);
        //yield match_res[i];
        //yield match_res[i].substr(id_start_po, id_end_po - id_start_po) + ":" + match_res[i].substr(ip_start_po, ip_end_po - ip_start_po);

    }
}





function getImg(inputStr) {
    if (inputStr != 0) { //出错则重新插入

    }

    var o = iter.next();
    if (o.done == false) {
        console.log(o.value);
        getImg(0);
    } else {
        //getPostHref();  
    }

}